package com.eco.fanliapp.utils;


import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLDecoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;


public class MD5Utils {

    private static final String ALGORIGTHM_MD5 = "MD5";
    private static final int CACHE_SIZE = 4096;

    public static String createMD5(String input) {
        try {
            return createMD5(input, "utf-8");
        } catch (Exception e) {
            return null;
        }
    }

    public static String createMD5(String input, String charset) throws Exception {
        byte[] data;
        if (charset != null && !"".equals(charset)) {
            data = input.getBytes(charset);
        } else {
            data = input.getBytes();
        }
        MessageDigest messageDigest = getMD5();
        messageDigest.update(data);
        return byteArrayToHexString(messageDigest.digest());
    }


    public static String generateFileMD5(String filePath) throws Exception {
        String md5 = "";
        File file = new File(filePath);
        if (file.exists()) {
            MessageDigest messageDigest = getMD5();
            InputStream in = new FileInputStream(file);
            byte[] cache = new byte[CACHE_SIZE];
            int nRead = 0;
            while ((nRead = in.read(cache)) != -1) {
                messageDigest.update(cache, 0, nRead);
            }
            in.close();
            byte data[] = messageDigest.digest();
            md5 = byteArrayToHexString(data);
        }
        return md5;
    }

    private static String byteArrayToHexString(byte[] data) {
        char hexDigits[] = {
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
        };
        char arr[] = new char[16 * 2];
        int k = 0;
        for (int i = 0; i < 16; i++) {
            byte b = data[i];
            arr[k++] = hexDigits[b >>> 4 & 0xf];
            arr[k++] = hexDigits[b & 0xf];
        }
        return new String(arr);
    }

    public static String MD5EncodeUtf8(String origin) {
        origin = origin + "geelysdafaqj23ou89ZXcj@#$@#$#@KJdjklj;D../dSF.,";
        return MD5Encode(origin, "utf-8");
    }

    /**
     * 返回大写MD5
     *
     * @param origin
     * @param charsetname
     * @return
     */
    private static String MD5Encode(String origin, String charsetname) {
        String resultString = null;
        try {
            resultString = new String(origin);
            MessageDigest md = MessageDigest.getInstance("MD5");
            if (charsetname == null || "".equals(charsetname))
                resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
            else
                resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));
        } catch (Exception exception) {
        }
        return resultString.toUpperCase();
    }


    private static MessageDigest getMD5() throws NoSuchAlgorithmException {
        return MessageDigest.getInstance(ALGORIGTHM_MD5);
    }

    public static final String getMD5(String str) {
        char[] hexDigits = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        try {
            byte[] e = str.getBytes();
            MessageDigest mdTemp = MessageDigest.getInstance("MD5");
            mdTemp.update(e);
            byte[] tmp = mdTemp.digest();
            char[] strs = new char[32];
            int k = 0;

            for (int i = 0; i < 16; ++i) {
                byte byte0 = tmp[i];
                strs[k++] = hexDigits[byte0 >>> 4 & 15];
                strs[k++] = hexDigits[byte0 & 15];
            }

            return (new String(strs)).toLowerCase();
        } catch (Exception var9) {
            return null;
        }
    }



    public static String m1(String str, String str2) {
        String c = g3(g2(g1(str)));
        return MD5Utils.getMD5(c + "&secretKey=" + str2);
    }

    public static String g3(Map<String, String> map) {
        StringBuilder sb = new StringBuilder();
        boolean z = true;
        if (map.size() > 0) {
            for (Map.Entry next : map.entrySet()) {
                String str = (String) next.getKey();
                String str2 = (String) next.getValue();
                try {
                    str2 = URLDecoder.decode(str2, "utf-8");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (z) {
                    z = false;
                } else {
                    sb.append("&");
                }
                sb.append(str);
                sb.append("=");
                sb.append(str2);
            }
        }
        return sb.toString();
    }

    public static Map<String, String> g2(Map<String, String> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }
        TreeMap treeMap = new TreeMap(new MapKeyComparator());
        treeMap.putAll(map);
        return treeMap;

    }

    public static Map<String, String> g1(String str) {
        if (str == null) {
            return null;
        }
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        try {
            String substring = str.substring(str.indexOf(63) + 1);
            if (isNull(substring)) {
                return linkedHashMap;
            }
            for (String str2 : substring.split("&")) {
                int indexOf = str2.indexOf("=");
                linkedHashMap.put(URLDecoder.decode(str2.substring(0, indexOf), "UTF-8"), str2.substring(indexOf + 1).replaceAll("\n", "").trim());
            }
            return linkedHashMap;
        } catch (Exception e) {

        }
        return linkedHashMap;
    }

    public static boolean isNull(String str) {
        return str == null || str.length() == 0 || str.equals("null");
    }
}